<template>
  <div class="dashboard-container">
    <el-card style="height: 120px; margin-bottom: 20px">
      <!-- 给el-form绑定form -->
      <el-form label-width="80px" :model="form" :inline="true">
        <!-- <el-form-item>
          <el-input v-model="form.name" placeholder="名称" clearable />
        </el-form-item> -->
        <!-- <el-button
          type="primary"
          icon="el-icon-search"
          @click="searchclick"
          style="margin-left: 12px"
          >筛选</el-button
        >
        <el-button @click="resetSearch">重置</el-button> -->
        <el-button @click="addrenyuan" type="success">添加服务分类</el-button>
      </el-form>
    </el-card>
    <el-table
      :data="tableData"
      border
      row-key="id"
      :tree-props="{ children: 'List', hasChildren: 'hasChildren' }"
    >
      <!-- style="width: 100%; font-size: 10px" -->
      <el-table-column prop="id" label="菜单ID" width="100"> </el-table-column>
      <el-table-column prop="name" label="名称" align="center">
      </el-table-column>
      <el-table-column label="封面图" align="center" width="180">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.image"
            fit="cover"
            style="width: 70px; height: 70px"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="classify" label="支付" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.classify == 1"
            >全额支付</el-tag
          >
          <el-tag type="warning" v-if="scope.row.classify == 2">付定金</el-tag>
          <el-tag type="danger" v-if="scope.row.classify == 3"
            >直接预约不付款</el-tag
          >
        </template>
      </el-table-column>

      <el-table-column prop="classify" label="首页展示对应产品" align="center">
        <!-- <template slot-scope="scope" v-if="!scope.row.switch111">
          <el-tag type="success" v-if="scope.row.index == 1">否</el-tag>
          <el-tag type="warning" v-if="scope.row.index == 2">是</el-tag>
        </template> -->
        <template slot-scope="scope" v-if="!scope.row.switch111">
          <el-switch
            v-model="scope.row.switch"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="change($event, scope.row.id)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="600">
        <template slot-scope="scope">
          <el-button
            type="success"
            @click="addadd(scope.row)"
            size="small"
            v-if="scope.row.switch111"
            >添加子服务分类</el-button
          >
          <!-- <el-button
            type="success"
            @click="zhanshi1(scope.row)"
            v-if="!scope.row.switch111"
            size="small"
            >首页分类图标展示</el-button
          >
          <el-button
            type="info"
            @click="zhanshi2(scope.row)"
            v-if="!scope.row.switch111"
            size="small"
            >首页展示对应产品</el-button
          > -->
          <el-button
            type="warning"
            @click="tiaozhuanguige(scope.row)"
            v-if="!scope.row.switch111"
            size="small"
            >服务列表</el-button
          >
          <!-- 编辑服务分类的 -->
          <el-button
            type="primary"
            @click="changerenyuan(scope.row)"
            v-if="!scope.row.images"
            size="small"
            >编辑</el-button
          >
          <!-- 编辑服务的 -->
          <!-- <el-button
            type="primary"
            @click="changerenyuan11(scope.row)"
            v-if="scope.row.images"
            size="small"
            >编辑</el-button
          > -->
          <el-button
            type="danger"
            @click="deldel(scope.row)"
            size="small"
            v-if="!scope.row.images"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :currentPage="this.form.pageNo"
      :size="this.form.pageSize"
      :total="total"
      @changelimit="changelimit"
      @changecurrentPage="changecurrentPage"
    ></Pagination>
    <add ref="add" @refresh="refresh"></add>
    <istuijian ref="istuijian" @refresh="refresh"></istuijian>
    <serveradd ref="serveradd" @refresh="refresh"></serveradd>
  </div>
</template>

<script>
import {
  getservelist,
  shangxiaservesxj,
  getservetypes,
  delservetypedel,
} from "@/api/server";
import { servetypeRecommend } from "@/api/servetypeIndex";
import add from "./modules/add";
import istuijian from "./modules/istuijian";
import serveradd from "./modules/serveradd";
export default {
  name: "Dashboard",
  components: {
    add,
    istuijian,
    serveradd,
  },
  data() {
    return {
      form: {
        servetypeid: "",
        pageNo: 1,
        pageSize: 5,
      },
      total: 0,
      tableData: [],
    };
  },
  methods: {
    async change(e, id) {
      let data = {
        id: Number(id),
        recommend: e ? 2 : 1,
        adminid:Number(localStorage.getItem('userId'))
      };
      console.log(data);
      let result = await servetypeRecommend(data);
      if (result.code == 0) {
        this.$message({
          showClose: true,
          message: "修改成功",
          type: "success",
        });
      }
      console.log(result);
      this.getservetypes1();
    },

    searchclick() {
      this.getservetypes1();
    },
    resetSearch() {
      this.form.name = "";
      this.getservetypes1();
    },
    addrenyuan() {
      this.$refs.add.add();
    },
    // 子传父:自定义事件
    refresh() {
      this.getservetypes1();
    },
    async getservetypes1() {
      let result = await getservetypes({adminid:Number(localStorage.getItem('userId'))});
      console.log(result);
      if (result.code == 0) {
        if (result.data != null) {
          this.tableData = result.data;
          this.tableData.forEach((item) => {
            this.$set(item, "switch111", true);

            if (item.List != null) {
              item.List.forEach((item1) => {
                if (item1.recommend == 2) {
                  this.$set(item1, "switch", true);
                } else {
                  this.$set(item1, "switch", false);
                }
              });
            }
          });
        }
        // this.$nextTick(() => {
        //   this.tableData.forEach(async (item, index) => {
        //     let result = await getservelist({
        //       servetypeid: item.id,
        //       pageNo: 1,
        //       pageSize: 50,
        //     });
        //     if (result.data.data != []) {
        //       this.$set(this.tableData[index], "List", result.data.data);
        //       this.tableData[index].List.forEach((item) => {
        //         if (item.status == 1) {
        //           this.$set(item, "switch", true);
        //         } else {
        //           this.$set(item, "switch", false);
        //         }
        //       });
        //       // console.log("在这里", this.tableData);
        //     }
        //   });
        // });
      }
    },
    changelimit(val) {
      this.form.pageSize = val;
      this.getservetypes1();
    },
    changecurrentPage(val) {
      this.form.pageNo = val;
      this.getservetypes1();
    },
    // 添加子服务分类--->需要添加的是父亲的id
    addadd(val) {
      console.log(val);
      this.$refs.add.edit2(val);
      // this.$refs.serveradd.add(val);
    },
    tiaozhuanguige(val) {
      this.$router.push({
        name: "server",
        query: {
          servetypeid: val.id,
        },
      });
    },
    changerenyuan(val) {
      console.log(111111111111111111111, val);
      this.$refs.add.edit(val);
    },
    changerenyuan11(val) {
      this.$refs.serveradd.edit(val);
    },
    isTuijian(val) {
      this.$refs.istuijian.edit(val);
    },
    async deldel(val) {
      let result = await delservetypedel({ id: val.id,adminid:Number(localStorage.getItem('userId')) });
      if (result.code == 0) {
        this.$message({
          showClose: true,
          message: "删除成功",
          type: "error",
        });
        this.getservetypes1();
      }
    },
  },
  watch: {
    "form.servetypeid": {
      handler(newVal) {
        if (newVal == "") {
          this.getservetypes1();
        }
      },
    },
  },
  filters: {
    statusfilter(val) {
      if (val == 1) {
        return "正常";
      } else {
        return "推荐";
      }
    },
    serverfilter(val) {
      if (that.reqcategory2 && that.reqcategory2.length > 0) {
        return that.reqcategory2.filter((item) => item.id == val)[0].name;
      } else {
        return "loading";
      }
    },
    datefilter(val) {
      var date = new Date(parseInt(val) * 1000);
      var Year = date.getFullYear();
      var Moth =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1;
      var Day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      var Hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
      var Minute =
        date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      var Sechond =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      var GMT =
        Year +
        "-" +
        Moth +
        "-" +
        Day +
        "   " +
        Hour +
        ":" +
        Minute +
        ":" +
        Sechond;

      return GMT;
    },
  },
  mounted() {
    this.getservetypes1();
  },
};
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 150px;
  height: 150px;
}

::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

::v-deep .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}

::v-deep .avatar {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;

  max-width: 150px;
  max-height: 150px;
  margin: 0 auto;
  display: block;
}
</style>
